package com.xy.stock.controller;

import com.xy.stock.config.RedisSingleUtil;
import com.xy.stock.entity.Course;
import com.xy.stock.mapper.CourceMapper;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/stock")
public class StockController {

    @Autowired
    private CourceMapper courceMapper;

    @Autowired
    private RedisSingleUtil redisSingleUtil;


    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public String getList(String key) {
        for(int i=0;i<5;i++) {
            int finalI = i;
            redisSingleUtil.lock("hahaAA_hhBB");
            new Thread(()->{
                redisSingleUtil.set("hahaAA:hhBB","hello:"+ finalI +":"+Thread.currentThread().getName());
            }).start();
            redisSingleUtil.unlock("hahaAA_hhBB");
        }
        return redisSingleUtil.get("hahaAA:hhBB");
    }



    @Transactional
    @RequestMapping(value = "/saveStockList", method = RequestMethod.POST)
    public String saveStockList() {
        Course c = new Course();
        c.setCname("stock");
        c.setUserId(Long.valueOf(2));
        c.setCstatus("1");
        courceMapper.insert(c);
        return "order ds_read success";
    }

}

